#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5e5 + 5;

int n;
int a[N], d[N], num;
int b[N], c[N], numb, numc;
int ans, L, R;
int bit[N];
int lowbit(int x) { return x & -x; }
void update(int x, int d) {
  for (int i = x; i <= num; i += lowbit(i)) bit[i] += d;
}
int query(int x) {
  int res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += bit[i];
  return res;
}

int f(int l, int r) {  // (l, r)
  if (l >= r) return -1;
  if (a[l] >= a[r]) return -1;
  if (L == R) L = r + 1, R = r;
  while (L < l) update(a[L++], -1);
  while (R > r) update(a[R--], -1);
  while (L > l) update(a[--L], 1);
  while (R < r) update(a[++R], 1);
  return 2 + query(a[r] - 1) - query(a[l]);
}

void solve(int l1, int r1, int l2, int r2) {
  if (l1 > r1 || l2 > r2) return;
  int p = (l1 + r1) / 2;
  int opt = -1;
  int mx = -1;
  rep(i, l2, r2) {
    int x = f(b[p], c[i]);
    if (x > mx) {
      ans = max(ans, x);
      mx = x;
      opt = i;
    }
  }
  if (opt == -1) {
    int q = l2;
    while (q <= r2 && c[q] < b[p]) q++;
    solve(l1, p - 1, l2, q - 1);
    solve(p + 1, r1, q, r2);
    return;
  }
  solve(l1, p - 1, l2, opt);
  solve(p + 1, r1, opt, r2);
}

void solve() {
  cin >> n;
  rep(i, 1, n) cin >> a[i];

  // discrete
  rep(i, 1, n) d[i] = a[i];
  sort(d + 1, d + n + 1);
  num = unique(d + 1, d + n + 1) - d - 1;
  rep(i, 1, n) a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d;
  // clear fenwich tree
  rep(i, 1, num) bit[i] = 0;

  numb = numc = 0;
  rep(i, 1, n) if (!numb || a[i] < a[b[numb]]) b[++numb] = i;
  per(i, n, 1) if (!numc || a[i] > a[c[numc]]) c[++numc] = i;
  reverse(c + 1, c + numc + 1);
  ans = 1;
  L = R = 0;
  solve(1, numb, 1, numc);
  cout << ans << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
